"""
Project Euler Problem 6: https://projecteuler.net/problem=6

Sum square difference

The sum of the squares of the first ten natural numbers is,
    1^2 + 2^2 + ... + 10^2 = 385

The square of the sum of the first ten natural numbers is,
    (1 + 2 + ... + 10)^2 = 55^2 = 3025

Hence the difference between the sum of the squares of the first ten
natural numbers and the square of the sum is 3025 - 385 = 2640.

Find the difference between the sum of the squares of the first one
hundred natural numbers and the square of the sum.
"""

# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = Thonny Python3.8
@creat_time = 2022/5/5
'''


def solution1(n: int = 100) -> int:
    '''
    square_sum(n) = (n*(n+1)/2)^2
    sum_square(n) = n*(n+1)*(2n+1)/6

    >>> solution1(10)
    2640
    >>> solution1(2)
    4
    >>> solution1(1)
    0
    '''
    
    square_sum = (n*(n+1)//2)**2
    sum_square = n*(n+1)*(2*n+1)//6
    
    return square_sum - sum_square


def solution2(n: int = 100) -> int:
    '''
    a_n - a_(n-1) = n^2 * (n-1)
    a_n = n^2 * (n-1) + (n-1)^2 * (n-2) + ... + 2^2 * 1
    
    >>> solution2(10)
    2640
    >>> solution2(2)
    4
    >>> solution2(1)
    0
    '''    
    # return sum([i*(i+1)**2 for i in range(1,n)])
    return sum(map(lambda i: i*(i+1)**2, range(1,n)))
    # 使用map运算效率更高


if __name__ == "__main__":
    import doctest

    doctest.testmod(verbose = True)
    
    
    print(f"{solution1() = }") 
    print(f"{solution2() = }")

    


